<template>
  <el-dialog :title="title" :visible="showDepats" @close="giveUp">
    <el-form ref="Form" :model="form" :rules="rules">
      <el-form-item prop="name" label="部门名称" :label-width="formLabelWidth">
        <el-input
          v-model="form.name"
          auto-complete="off"
          placeholder="1-50个字符"
        ></el-input>
      </el-form-item>
      <el-form-item prop="code" label="部门编码" :label-width="formLabelWidth">
        <el-input
          v-model="form.code"
          auto-complete="off"
          placeholder="1-50个字符"
        ></el-input>
      </el-form-item>
      <el-form-item
        prop="manager"
        label="部门负责人"
        :label-width="formLabelWidth"
      >
        <el-select
          v-model="form.manager"
          placeholder="请选择负责人"
          style="width:100%"
          @focus="getEmpSim"
        >
          <el-option
            v-for="item in empSimlist"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        prop="introduce"
        label="部门介绍"
        :label-width="formLabelWidth"
      >
        <el-input
          type="textarea"
          rows="4"
          placeholder="1-300个字符"
          v-model="form.introduce"
          auto-complete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="giveUp">取 消</el-button>
      <el-button type="primary" @click="commitadd">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getDepartments,
  addDepartment,
  getDepartmentById,
  editDepartment
} from '@/api/departments'
import { getEmpSimple } from '@/api/employees'
export default {
  name: 'adddet',
  data () {
    let checkName = async (rules, value, callback) => {
      // 对部门名称进行重名验证
      //   发出请求,获取所有数据
      let res = await getDepartments()
      // 如果是编辑
      if (this.form.id) {
        // 过滤出同级的所有元素
        let list = res.data.depts.filter(item => item.pid == this.form.pid)
        list.some(item => item.name == value && item.id != this.form.id)
          ? callback(new Error('该部门名称已存在,请重新输入'))
          : callback()
        // 判断是否重名
      } else {
        //   找出所有的子节点
        let list = res.data.depts.filter(item => item.pid == this.currtenId)
        //   遍历子节点查看是否有重名
        list.forEach(item => {
          if (value == item.name) {
            callback(new Error('该部门名称已存在,请重新输入'))
          }
        })
      }
      callback()
    }
    //code自定义校验
    let checkCode = async (rules, value, callback) => {
      let res = await getDepartments()
      // console.log(res)
      // 判断是编辑还是新增
      if (this.form.id) {
        // 是编辑
        res.data.depts.some(
          item => item.code == value && item.id != this.form.id
        )
          ? callback(new Error('该编码已存在, 请重新输入'))
          : callback()
      } else {
        // 是新增
        res.data.depts.some(item => item.code == value)
          ? callback(new Error('该编码已存在, 请重新输入'))
          : callback()
      }
    }
    return {
      empSimlist: [],
      form: {},
      formLabelWidth: '120px',
      //   表单校验规则
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入1-50个字符', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '请选择负责人', trigger: 'bulr' }],
        introduce: [
          { required: true, message: '部门描述不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '请输入1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    showDepats: {
      type: Boolean,
      default: true
    },
    //点击的节点的id
    currtenId: {
      type: String,
      default: ''
    }
  },
  methods: {
    async getEmpSim () {
      // debugger
      let res = await getEmpSimple()
      // console.log(res)
      this.empSimlist = res.data
    },
    commitadd () {
      this.$refs.Form.validate(async v => {
        // console.log(v)
        // console.log(this.form)
        if (v) {
          // 先处理,给form添加上id
          let data = { ...this.form, pid: this.currtenId }
          //验证通过发起请求
          // 如果是新增
          if (this.form.id) {
            await editDepartment(this.form)
            // this.$message.success('修改成功')
          } else {
            await addDepartment(data)
          }
          // 提示
          this.$message.success(this.title + '新增成功')
          // 隐藏新增弹框,
          this.$emit('update:showDepats', false)
          // 重新请求数据刷新页面
          this.$emit('addsuce')
        } else {
        }
      })
    },
    async giteditmsg (id) {
      let res = await getDepartmentById(id)
      this.form = res.data
      // console.log(res)
    },
    giveUp () {
      this.$emit('giveup')
      this.form = {}
      // 调用重置事件，不然校验提示还在
      this.$refs.Form.resetFields()
    }
  },
  computed: {
    title: function () {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  }
}
</script>

<style></style>
